package com.hua.ui;

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Image;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Font;
import java.awt.Color;
import java.awt.SystemColor;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JTextField;

import com.hua.Dao.SqlService;
import com.hua.Model.UserInfo;
import com.hua.Service.funtion;

import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JProgressBar;

public class setting extends JPanel {
	private int width = 130, height = 130;
	private ImageIcon image;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;

	private String profile_photo = "";
	private String name = "";
	private String account = "";
	private String password = "";

	funtion funtion = new funtion();

	public setting(UserInfo user, int permiss) {

		setBackground(SystemColor.activeCaption);
		setForeground(SystemColor.activeCaption);
		setLayout(null);

		JPanel panel = new JPanel();
		panel.setBackground(SystemColor.textHighlightText);
		panel.setBounds(0, 0, 912, 540);
		add(panel);
		panel.setLayout(null);

		JLabel lblNewLabel_1 = new JLabel("账户设置");
		lblNewLabel_1.setFont(new Font("Alimama ShuHeiTi", Font.BOLD, 25));
		lblNewLabel_1.setBounds(14, 13, 133, 40);
		panel.add(lblNewLabel_1);

		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setBounds(24, 67, 130, 130);
		panel.add(lblNewLabel);

		if (!user.getImg().equals("https://pic.imgdb.cn/item/654dbe5ac458853aef3328a9.png")) {
			// 头像无更改，使用默认减少网络请求，因为是同步请求，会阻塞进程
			try {
				URL url = new URL(user.getImg());
				BufferedImage img = ImageIO.read(url);
				Image scaledImg = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
				ImageIcon icon = new ImageIcon(scaledImg);
				lblNewLabel.setIcon(icon);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			image = new ImageIcon("src/main/resources/images/logo.png");
			Image img = image.getImage();
			img = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
			image.setImage(img);
			lblNewLabel.setIcon(image);
		}

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(Color.WHITE);
		panel_1.setBounds(182, 28, 716, 512);
		panel.add(panel_1);
		panel_1.setLayout(null);

		JPanel panel_2 = new JPanel();
		panel_2.setBounds(14, 13, 608, 54);
		panel_1.add(panel_2);
		panel_2.setLayout(null);

		JLabel lblNewLabel_2 = new JLabel("昵称");
		lblNewLabel_2.setFont(new Font("Alimama ShuHeiTi", Font.PLAIN, 20));
		lblNewLabel_2.setBounds(14, 17, 90, 24);
		panel_2.add(lblNewLabel_2);

		textField = new JTextField();
		textField.setFont(new Font("Alimama ShuHeiTi", Font.PLAIN, 16));
		textField.setForeground(SystemColor.inactiveCaption);
		textField.setText(user.getName());
		textField.setBounds(105, 8, 489, 39);
		panel_2.add(textField);
		textField.setColumns(10);

		JPanel panel_2_1 = new JPanel();
		panel_2_1.setLayout(null);
		panel_2_1.setBounds(14, 147, 608, 54);
		panel_1.add(panel_2_1);

		JLabel lblNewLabel_2_1 = new JLabel("修改密码");
		lblNewLabel_2_1.setFont(new Font("Alimama ShuHeiTi", Font.PLAIN, 20));
		lblNewLabel_2_1.setBounds(14, 17, 90, 24);
		panel_2_1.add(lblNewLabel_2_1);

		textField_1 = new JTextField();
		textField_1.setText("******");
		textField_1.setForeground(SystemColor.inactiveCaption);
		textField_1.setFont(new Font("Alimama ShuHeiTi", Font.PLAIN, 16));
		textField_1.setColumns(10);
		textField_1.setBounds(105, 8, 489, 39);
		panel_2_1.add(textField_1);

		JPanel panel_2_1_1 = new JPanel();
		panel_2_1_1.setLayout(null);
		panel_2_1_1.setBounds(14, 214, 608, 54);
		panel_1.add(panel_2_1_1);

		JLabel lblNewLabel_2_1_1 = new JLabel("标题栏目");
		lblNewLabel_2_1_1.setFont(new Font("Alimama ShuHeiTi", Font.PLAIN, 20));
		lblNewLabel_2_1_1.setBounds(14, 17, 90, 24);
		panel_2_1_1.add(lblNewLabel_2_1_1);

		textField_2 = new JTextField();
		textField_2.setText(user.getSign());
		textField_2.setForeground(SystemColor.inactiveCaption);
		textField_2.setFont(new Font("Alimama ShuHeiTi", Font.PLAIN, 16));
		textField_2.setColumns(10);
		textField_2.setBounds(105, 8, 489, 39);
		panel_2_1_1.add(textField_2);

		JButton btnNewButton = new JButton("保存");
		btnNewButton.setForeground(Color.ORANGE);
		btnNewButton.setFont(new Font("Alimama ShuHeiTi", Font.BOLD, 15));
		btnNewButton.setBounds(558, 448, 115, 44);
		panel_1.add(btnNewButton);

		JCheckBox chckbxNewCheckBox = new JCheckBox("我真的已认真阅读并同意《改不改都会被盗号》协议");
		chckbxNewCheckBox.setSelected(true);
		chckbxNewCheckBox.setBounds(14, 465, 379, 27);
		panel_1.add(chckbxNewCheckBox);

		JRadioButton rdbtnNewRadioButton = new JRadioButton("我参加但没有的用户体验提升计划");
		rdbtnNewRadioButton.setSelected(true);
		rdbtnNewRadioButton.setBounds(14, 431, 309, 27);
		panel_1.add(rdbtnNewRadioButton);

		JPanel panel_2_2 = new JPanel();
		panel_2_2.setLayout(null);
		panel_2_2.setBounds(14, 80, 608, 54);
		panel_1.add(panel_2_2);

		JLabel lblNewLabel_2_2 = new JLabel("账户");
		lblNewLabel_2_2.setFont(new Font("Alimama ShuHeiTi", Font.PLAIN, 20));
		lblNewLabel_2_2.setBounds(14, 17, 90, 24);
		panel_2_2.add(lblNewLabel_2_2);

		textField_3 = new JTextField();
		textField_3.setText(user.getAccount());
		textField_3.setForeground(SystemColor.inactiveCaption);
		textField_3.setFont(new Font("Alimama ShuHeiTi", Font.PLAIN, 16));
		textField_3.setColumns(10);
		textField_3.setBounds(105, 8, 489, 39);
		panel_2_2.add(textField_3);

		JButton btnNewButton_2 = new JButton("注销账户");
		btnNewButton_2.setBackground(Color.WHITE);
		btnNewButton_2.setForeground(Color.RED);
		btnNewButton_2.setFont(new Font("Alimama ShuHeiTi", Font.BOLD, 15));
		btnNewButton_2.setBounds(429, 448, 115, 44);
		panel_1.add(btnNewButton_2);

		JLabel lblNewLabel_3 = new JLabel("");
		image = new ImageIcon("src/main/resources/images/banner.png");
		// image.setImage(image.getImage().getScaledInstance(width,
		// height,Image.SCALE_DEFAULT));
		Image img_banner = image.getImage();
		img_banner = img_banner.getScaledInstance(610, 200, Image.SCALE_AREA_AVERAGING);
		image.setImage(img_banner);
		lblNewLabel_3.setIcon(new ImageIcon(img_banner));
		lblNewLabel_3.setBounds(14, 281, 653, 141);
		panel_1.add(lblNewLabel_3);

		JButton btnNewButton_1 = new JButton("更改头像");
		btnNewButton_1.setFont(new Font("Alimama ShuHeiTi", Font.BOLD, 15));
		btnNewButton_1.setBounds(24, 210, 130, 40);
		panel.add(btnNewButton_1);

		// 存储空间计算
		funtion funtion = new funtion();
		int Storage = funtion.query_storage(user.getId());
		int proportion = (int) Math.round((double) Storage / user.getStorage() * 100);

		JProgressBar progressBar = new JProgressBar();
		progressBar.setBounds(14, 500, 154, 14);

		// 设置进度条的最小值和最大值
		progressBar.setMinimum(0);
		progressBar.setMaximum(100);

		// 设置进度条的初始值
		progressBar.setValue(proportion);

		// 设置进度条显示数字
		progressBar.setStringPainted(true);
		panel.add(progressBar);

		JLabel lblNewLabel_4 = new JLabel("存储空间:\n" + Storage + "/" + user.getStorage() + " MB");
		lblNewLabel_4.setBounds(14, 452, 169, 40);
		panel.add(lblNewLabel_4);
		
		JLabel lblNewLabel_5 = new JLabel("下载路径:默认");
		lblNewLabel_5.setFont(new Font("Alimama ShuHeiTi", Font.PLAIN, 15));
		lblNewLabel_5.setBounds(14, 436, 113, 18);
		panel.add(lblNewLabel_5);

		// 更新头像
		btnNewButton_1.addActionListener(e -> {
			GetStringWindow GetTextwindow = new GetStringWindow("修改头像", "请在下方输入新的头像的Url直链", user.getImg());
			GetTextwindow.setVisible(true);
			String result = GetTextwindow.getGetText();
			if (result != null && !result.equals("") && !result.equals(user.getImg())
					&& funtion.check_str(result, "网址")) {
				profile_photo = result;
			} else if (result != null) {
				funtion.tips("头像更改", "你输入的不符合直链规范");
			}
		});
		// 注销账户
		btnNewButton_2.addActionListener(e -> {
			if (permiss == 1) {
				GetStringWindow GetTextwindow = new GetStringWindow("我们尊重你的选择，一旦注销则会删除所有你的所有相关数据且不可恢复!",
						"请在下方输入: “确认注销" + user.getName() + "”以确认", "");
				GetTextwindow.setVisible(true);
				String result = GetTextwindow.getGetText();
				if (result != null && result.equals("确认注销" + user.getName())) {
					// 注销用户
					System.out.println("注销用户操作");
					String status = funtion.WriteOffUser(user.getAccount(), user.getPassword());
					if (status.equals("用户已注销")) {
						funtion.wanr_tips("账号注销", "恨君不似江楼月，南北东西，南北东西，只有相随无别离");
						// 自动关闭软件，防止意外的操作事件执行
						try {
							Thread.sleep(5000);
							System.exit(0);
						} catch (InterruptedException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} finally {
							System.exit(0);
							// 延迟5秒（5000毫秒）
						}
					}
				} else if (result != null) {
					funtion.tips("注销取消", "未确认注销!");
				}
			} else {
				funtion.faild_tips("注销用户", "抱歉，你无权注销账户!");
			}
		});
		// 保存设置
		btnNewButton.addActionListener(e -> {
			if (permiss != 1) {
				funtion.faild_tips("更改信息", "抱歉，你无权更改信息!");
			} else {
				List<String> sqlList = new ArrayList<>();
				String name = textField.getText();
				String account = textField_3.getText();
				String password = textField_1.getText();
				String sign = textField_2.getText();
				String modify = "";
				if (!account.equals(user.getAccount()) && account != null && !account.equals("")) {
					if (funtion.check_str(account, "账户") && !funtion.check_str(account, "含中文") && account.length() <= 30
							&& account.length() >= 11) {
						System.out.println(account);
						modify += "\n账户:" + account;
						if (!funtion.is_account(account)) {
							sqlList.add("account='" + account + "'");
						} else {
							funtion.wanr_tips("账户名更新", "该账户名已注册!");
						}
					} else {
						funtion.wanr_tips("账户名更新", "账户输入异常!");
					}
				}
				if (!profile_photo.equals("")) {
					modify += "\n头像:" + profile_photo;
					sqlList.add("img='" + profile_photo + "'");
				}
				if (!password.equals("") && !password.equals("******") && password.length() >= 6) {
					modify += "\n密码:" + password;
					sqlList.add("password='" + funtion.encryptSHA256(password) + "'");
				}
				if (!name.equals("") && !name.equals(user.getName())) {
					modify += "\n昵称:" + name;
					sqlList.add("name='" + name + "'");
				}
				if (!sign.equals("") && !sign.equals(user.getSign())) {
					modify += "\n标语:" + sign;
					sqlList.add("sign='" + sign + "'");
				}

				String sql = String.join(",", sqlList);
				if (!sql.equals("") && funtion.areYouOk_tips("更改信息", "你将更新这些内容：" + modify + "\n确定吗？") == 0) {
					SqlService sqlService = new SqlService();
					System.out.println(sql);
					Boolean update = sqlService.update_SQL("Users", sql, user.getId());
					if (update) {
						funtion.tips("更新信息", "更新成功!\n部分信息在下次打开软件才能展示");
					}
				} else {
					funtion.wanr_tips("更新内容", "你好像什么都没改...\n或者改得很离谱");
				}
				// funtion.tips("更改设置", "更改");
			}
		});
	}
}
